﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Interface.weixin.Html
{
    public partial class WxCompleteMemberInfoHtml : UrlRewritePage
    {
        public string owner;
        public string openId;
        public string sn;
        public string prizeName;
        public string awardId;
        public string _msg;
        public string gameId;
        public string _url;

        protected void Page_Load(object sender, EventArgs e)
        {
            owner = Request["account"];
            openId = Request["openid"];
            gameId = Request["gameid"];
            sn = Request["sn"];
            prizeName = Request["prizename"];
            awardId = Request["awardid"];
            _url=Request["url"];
            if (sn != null)
            {
                Response.Write("恭喜你中奖了! 奖品:" + prizeName + "  SN码:" + sn + "<br/>" + "请完善个人信息:");
            }
            else
            {
                Response.Write("请完善个人信息:");
            }
        }

        #region 提交信息 + protected void btnSubmit_Click(object sender, EventArgs e)
        /// <summary>
        /// 提交信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MemberInfo _member = new MemberInfo();

            if (!string.IsNullOrEmpty(owner) && !string.IsNullOrEmpty(openId))
            {
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    try
                    {
                        _member = session.CreateQuery("from MemberInfo where OwnerAccount=:owner and OpenId=:wxid")
                           .SetParameter("owner", owner)
                           .SetParameter("wxid", openId)
                           .UniqueResult<MemberInfo>();

                        if (_member == null)//新建
                        {
                            _member = new MemberInfo();
                            _member.OwnerAccount = owner;
                            _member.OpenId = openId;
                            _member.MemberName = txtName.Text.Trim();
                            _member.Telephone = txtTel.Text.Trim();
                            _member.Email = txtEmail.Text.Trim();
                        }
                        else
                        {
                            _msg = "您已经完善个人信息";
                            return;
                        }
                        
                        session.SaveOrUpdate(_member);
                        session.Flush();

                        if (sn == "")
                        {
                            Response.Redirect("/Interface/weixin/Html/WxApplyMemberCardHtml.aspx?account=" + owner + "&openid=" + openId);
                        }
                        else
                        {
                            //注册用户
                            WxGamesAward gameaward = session.CreateCriteria<WxGamesAward>().Add(Expression.Eq("OwnerAccount", owner)).Add(Expression.Eq("Id",int.Parse( awardId))).UniqueResult<WxGamesAward>();

                            gameaward.ApplyDate = DateTime.Now;
                            gameaward.AwardStatus = "1";
                            gameaward.WxId = openId;
                           
                            session.Update(_member);
                            session.Flush();

                            _msg = "提交成功!请牢记您的SN码.";
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script type='text/javascript'>alert('" + ex.Message + "');</script>");
                    }

                }
            }
        }
        #endregion

        #region 设置返回页面路径 + protected void btnBack_Click(object sender, EventArgs e)
        /// <summary>
        /// 设置返回页面路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
            switch (_url)
            {
                case "bigwheel":
                    Response.Redirect("/Interface/weixin/Game/BigWheel/BigWheel.aspx?uid=" + gameId + "&account=" + owner + "&openid=" + openId);
                    break;
                case "fruit":
                    Response.Redirect("/Interface/weixin/Game/Fruit/Fruit.aspx?uid=" + gameId + "&account=" + owner + "&openid=" + openId);
                    break;
                case "scratch":
                    Response.Redirect("/Interface/weixin/Game/Scratch/Scratch.aspx?uid=" + gameId + "&account=" + owner + "&openid=" + openId);
                    break;
                case "membercard":
                    Response.Redirect("/Interface/weixin/Html/WxMemberCardHtml.aspx?account=" + owner + "&openid=" + openId);
                    break;
            }
        } 
        #endregion
    }
}